import com.sun.media.ui.MessageBox;
import de.javasoft.plaf.synthetica.SyntheticaBlueIceLookAndFeel;
import java.io.File;
import javax.swing.JFileChooser;
import javax.swing.UIManager;

/**
 * Represents the main window of the application
 */
public class GUI extends javax.swing.JFrame {
    
    // Static reference to the main application frame
    public static GUI frame;
    
    /**
     * Class constructor
     */
    public GUI() {
        initComponents();
        this.setTitle("Locksmith");
        this.setResizable(false);
        this.setLocationRelativeTo(null);
        this.jMenuItemKeyManager.setText("Key Manager");
        this.jMenuItemExit.setText("Exit");
        GUI.frame = this;
        Settings.loadConfiguration();
    }
    
    /**
     * Main application function
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    UIManager.setLookAndFeel(new SyntheticaBlueIceLookAndFeel());
                } catch (Exception e) {
                    e.printStackTrace();
                }
                new GUI().setVisible(true);
            }
        });
        
        SyntheticaBlueIceLookAndFeel.setAntiAliasEnabled(true);
    }
    
    /**
     * Processes a requested source file into a requested destination file
     * @param is_encrypt Specifies if it's an encryption or decryption operation
     */
    private void processFile(boolean is_encrypt) {
        JFileChooser fc = new JFileChooser();
        String path_source = new String("");
        String path_destination = new String("");
        
        fc.setFileSelectionMode(JFileChooser.FILES_ONLY);
        
        // If the OK button was opened on the source file request
        if (fc.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {
            path_source = fc.getSelectedFile().getAbsolutePath();
            
            // If the OK button was opened on the destination file request
            if (fc.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {
                path_destination = fc.getSelectedFile().getAbsolutePath();
                
                // Encrypt or Decrypt the selected file
                if(is_encrypt)
                    new Encrypter().encryptFile(path_source,path_destination);
                else
                    new Encrypter().decryptFile(path_source,path_destination);
            }
        }
    }
    
    /**
     * Enables or disables the interface
     * @param enabled Indicates the desired state of the interface
     */
    public void enableInterface(boolean enabled) {
        btnDecrypt.setEnabled(enabled);
        btnEncrypt.setEnabled(enabled);
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
    private void initComponents() {
        buttonGroup1 = new javax.swing.ButtonGroup();
        btnEncrypt = new javax.swing.JButton();
        btnDecrypt = new javax.swing.JButton();
        jMenuBar = new javax.swing.JMenuBar();
        jMenuFile = new javax.swing.JMenu();
        jMenuItemKeyManager = new javax.swing.JMenuItem();
        jMenuItemExit = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
        setTitle("JCrypt");
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosed(java.awt.event.WindowEvent evt) {
                formWindowClosed(evt);
            }
        });

        btnEncrypt.setText("Encrypt");
        btnEncrypt.setEnabled(false);
        btnEncrypt.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                btnEncryptMousePressed(evt);
            }
        });

        btnDecrypt.setText("Decrypt");
        btnDecrypt.setEnabled(false);
        btnDecrypt.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                btnDecryptMousePressed(evt);
            }
        });

        jMenuFile.setText("Menu");
        jMenuItemKeyManager.setText("Item");
        jMenuItemKeyManager.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                jMenuItemKeyManagerMousePressed(evt);
            }
        });

        jMenuFile.add(jMenuItemKeyManager);

        jMenuItemExit.setText("Item");
        jMenuItemExit.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                jMenuItemExitMousePressed(evt);
            }
        });

        jMenuFile.add(jMenuItemExit);

        jMenuBar.add(jMenuFile);

        setJMenuBar(jMenuBar);

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(btnEncrypt)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 15, Short.MAX_VALUE)
                .add(btnDecrypt)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                .addContainerGap(12, Short.MAX_VALUE)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(btnEncrypt)
                    .add(btnDecrypt, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 23, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );
        pack();
    }// </editor-fold>//GEN-END:initComponents
    
    /**
     * Event invoked when the main window is closed
     */
    private void formWindowClosed(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosed
        Settings.saveConfiguration();
    }//GEN-LAST:event_formWindowClosed
    
    /**
     * Event invoked when the decrypt button is pressed
     */
    private void btnDecryptMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btnDecryptMousePressed
        if(btnDecrypt.isEnabled())
            processFile(false);
    }//GEN-LAST:event_btnDecryptMousePressed
    
    /**
     * Event invoked when the encrypt button is pressed
     */
    private void btnEncryptMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btnEncryptMousePressed
        if(btnEncrypt.isEnabled())
            processFile(true);
    }//GEN-LAST:event_btnEncryptMousePressed
    
    /**
     * Event invoked when the menu "Exit" button is pressed
     */
    private void jMenuItemExitMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jMenuItemExitMousePressed
        this.dispose();
    }//GEN-LAST:event_jMenuItemExitMousePressed
    
    /**
     * Event invoked when the menu "Key Manager" button is pressed
     */
    private void jMenuItemKeyManagerMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jMenuItemKeyManagerMousePressed
        new GUIKeyManager().setVisible(true);
        this.setVisible(false);
    }//GEN-LAST:event_jMenuItemKeyManagerMousePressed
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnDecrypt;
    private javax.swing.JButton btnEncrypt;
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.JMenuBar jMenuBar;
    private javax.swing.JMenu jMenuFile;
    private javax.swing.JMenuItem jMenuItemExit;
    private javax.swing.JMenuItem jMenuItemKeyManager;
    // End of variables declaration//GEN-END:variables
}